/**
 * 
 */
package com.newbsd.web.maventest.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.omg.CORBA.ULongLongSeqHelper;

import com.newbsd.web.maventest.dao.ConnectDb;
import com.newbsd.web.maventest.dao.ManagerDao;
import com.newbsd.web.maventest.pojo.Manager;
import com.newbsd.web.maventest.pojo.User;

/**
 * @author Mei
 *
 */
public class ManagerDaoImpl implements ManagerDao {

	/**
	 * 管理员登录
	 */
	public Manager login(String mName, String mPwd) {
		Manager manager = null;
		Connection conn = ConnectDb.Connect();
		
		//执行事务
		if (null != conn ) {
			ResultSet rs = null;
			try {
				PreparedStatement pstmt = conn.prepareStatement("select * from manager where mName=? and mPassword=?");
				pstmt.setString(1, mName);	
				pstmt.setString(2, mPwd);
				
				rs = pstmt.executeQuery();
				while (rs.next()) {
					manager = new Manager();
					manager.setManagerId(rs.getInt("managerId"));
					manager.setmName(rs.getString("mName"));
					manager.setmPassword(rs.getString("mPassword"));
					manager.setSex(rs.getString("sex"));
					manager.setmRealname(rs.getString("mRealname"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return manager;
	}
		

	/**
	 * 查询所有管理员信息
	 */
	public Manager getAll() {
		Manager manager = null;
		Connection conn = ConnectDb.Connect();
		//执行事务
		if (null != conn ) {
			ResultSet rs = null;
			try {
				Statement st = conn.createStatement();
				rs = st.executeQuery("select * from manager");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			if (null != rs) {
				try {
					
					while (rs.next()) {
						manager = new Manager();
						manager.setManagerId(rs.getInt("managerId"));
						manager.setmName(rs.getString("mName"));
						manager.setmPassword(rs.getString("mPassword"));
						manager.setSex(rs.getString("sex"));
						manager.setmRealname(rs.getString("mRealname"));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return manager;
	}
	//获取自己的信息
	public Manager getManager(Manager m) {
		//Manager manager = null;
		
		Connection conn = ConnectDb.Connect();
		if (null != conn ) {
			ResultSet rs = null; 
			try {
				Statement st = conn.createStatement();
				rs = st.executeQuery("select * from manager where mName="+m.getmName()+"and mPwd="+m.getmPassword());
				
				while (rs.next()) {
					
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
				
		return m;
		
	}

	/**
	 * 修改管理员信息
	 */
	public void updateManager(Manager  manager) {
		Connection conn = ConnectDb.Connect();
		if (null != conn ) {
			try {
				PreparedStatement pstmt = conn.prepareStatement("update manager set mName=?,mPassword=?,sex=? where managerId = ?");
				pstmt.setString(1, manager.getmName());
				pstmt.setString(2, manager.getmPassword());
				pstmt.setString(3, manager.getSex());
				pstmt.setInt(4, manager.getManagerId());
				
				pstmt.executeUpdate();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	
	
	

}
